<template>
  <div class="app-container">

    <el-form :model="queryParam" ref="queryForm" size="small" :inline="true"  label-width="68px">
      <el-form-item label="字典名称" prop="dictType">
        <el-select v-model="queryParam.dictType"   >
          <el-option
            v-for="item in dictTypeOptions"
            :key="item.dictType"
            :label="item.dictName"
            :value="item.dictType"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="字典标签" prop="dictLabel">
        <el-input
          v-model="queryParam.dictLabel"
          placeholder="请输入字典标签"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParam.status" placeholder="数据状态" clearable>
          <el-option
            v-for="dict in dicts.sys_normal_disable"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
     <el-form-item>
       <el-button type="primary" icon="el-icon-search" size="mini" @click="btnSearch">搜索</el-button>
     </el-form-item>
    </el-form>


  <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd()"
            v-hasPerm="['system:dict:add']"
          >新增</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="success"
            plain
            icon="el-icon-edit"
            size="mini"
            :disabled="single"
            @click="handleUpdate()"
            v-hasPerm="['system:dict:edit']"
          >修改</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="danger"
            plain
            icon="el-icon-delete"
            size="mini"
            :disabled="multiple"
            @click="handleDelete()"
            v-hasPerm="['system:dict:remove']"
          >删除</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="warning"
            plain
            icon="el-icon-download"
            size="mini"
            @click="handleExport"
            v-hasPerm="['system:dict:export']"
          >导出</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="warning"
            plain
            icon="el-icon-close"
            size="mini"
            @click="handleClose"
          >关闭</el-button>
        </el-col>

      </el-row>

    <el-table :data="tableData.rows" border style="width: 100%" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="字典编码" align="center" prop="dictCode" />
      <el-table-column label="字典标签" align="center" prop="dictLabel">
        <template slot-scope="scope">
          <span v-if="scope.row.listClass == '' || scope.row.listClass == 'default'">{{scope.row.dictLabel}}</span>
          <el-tag v-else :type="scope.row.listClass == 'primary' ? '' : scope.row.listClass">{{scope.row.dictLabel}}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="字典键值" align="center" prop="dictValue" />
      <el-table-column label="字典排序" align="center" prop="dictSort" />
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
            <DictTag :dataArray="dicts.sys_normal_disable" :value="scope.row.status"></DictTag>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" :show-overflow-tooltip="true" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="180"></el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPerm="['system:dict:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPerm="['system:dict:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination v-if="tableData.total>0" :current-page="queryParam.page" :page-sizes="[10, 20,30,50]"
      :page-size="queryParam.size" layout="total, sizes, prev, pager, next, jumper" :total="tableData.total"
      @size-change="handleSizeChange" @current-change="handleCurrentChange" />


  <!-- 添加或修改参数配置对话框 -->
  <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body :close-on-click-modal="false"
      :before-close="cancelForm">
    <el-form ref="form" :model="form" :rules="rules" label-width="80px">
      <el-form-item label="字典类型">
        {{currentDictName}}
      </el-form-item>
      <el-form-item label="数据标签" prop="dictLabel">
        <el-input v-model="form.dictLabel" placeholder="请输入数据标签" />
      </el-form-item>
      <el-form-item label="数据键值" prop="dictValue">
        <el-input v-model="form.dictValue" placeholder="请输入数据键值" />
      </el-form-item>
      <el-form-item label="样式属性" prop="cssClass">
        <el-input v-model="form.cssClass" placeholder="请输入样式属性" />
      </el-form-item>
      <el-form-item label="显示排序" prop="dictSort">
        <el-input-number v-model="form.dictSort" controls-position="right" :min="0" />
      </el-form-item>
      <el-form-item label="回显样式" prop="listClass">
        <el-select v-model="form.listClass">
          <el-option
            v-for="item in listClassOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-radio-group v-model="form.status">
          <el-radio
            v-for="dict in dicts.sys_normal_disable"
            :key="dict.dictValue"
            :label="dict.dictValue"
          >{{dict.dictLabel}}</el-radio>
        </el-radio-group>
      </el-form-item>

      <el-form-item label="默认值" prop="isDefault">
        <el-radio-group v-model="form.isDefault">
          <el-radio
            v-for="dict in dicts.sys_yes_no"
            :key="dict.dictValue"
            :label="dict.dictValue"
          >{{dict.dictLabel}}</el-radio>
        </el-radio-group>
      </el-form-item>

      <el-form-item label="备注" prop="remark">
        <el-input v-model="form.remark" type="textarea" placeholder="请输入内容"></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
     <el-button type="primary" @click="submitForm">确 定</el-button>
     <el-button @click="cancelForm">取 消</el-button>
    </div>
  </el-dialog>


  </div>
</template>

<script>
  import {optionselect, findByIdApi} from '@/api/dicttype.js'
  import {dataListApi,addApi, editApi, deleteApi, findByIdApi as findDataById} from '@/api/dictdata.js'
  import PageMinix from '@/mixin/PageMixin.js'
  import AddOrEditMixin from '@/mixin/AddOrEditMixin.js'
  import DictDataMinix from '@/mixin/DictDataMinix.js'
  export default {
    mixins: [DictDataMinix, PageMinix, AddOrEditMixin],
    dicts: ['sys_normal_disable','sys_yes_no'],
    data() {
      return {
        fetchDataApi: dataListApi,
        addApi: addApi,
        editApi: editApi,
        deleteByIdApi: deleteApi,
        findByIdApi: findDataById,
        primaryKey: 'dictCode',
        //所有的字典类型
        dictTypeOptions: [],
        // 保存默认选中的字典类型ID
        defaultDictId: undefined,
        //当前选中的字典名称
        currentDictName: undefined,
        // 默认不要加载混入的初始化方法
        executeFetchData: false,
        // 数据标签回显样式（element-ui样式）
        listClassOptions: [
          {
            value: "default",
            label: "默认"
          },
          {
            value: "primary",
            label: "主要"
          },
          {
            value: "success",
            label: "成功"
          },
          {
            value: "info",
            label: "信息"
          },
          {
            value: "warning",
            label: "警告"
          },
          {
            value: "danger",
            label: "危险"
          }
        ],
        // 表单校验
        rules: {
          dictLabel: [
            { required: true, message: "数据标签不能为空", trigger: "blur" }
          ],
          dictValue: [
            { required: true, message: "数据键值不能为空", trigger: "blur" }
          ],
          dictSort: [
            { required: true, message: "数据顺序不能为空", trigger: "blur" }
          ]
        }
      }
    },
    methods: {
     initFormValue() {
       this.form = {
         status: '0',
         isDefault: 'N'
       }
       //当前选中的分类名称
       this.currentDictName =undefined

     },
     // 导出excel
     handleExport(){

     },
     //加载所有的字典类型数据
     loadDictType(){
       optionselect().then(resp => {
         this.dictTypeOptions= resp.data
       })
     },
     // 界面第一次加载的时候，根据dictId查询它的类型名称
     loadDictTypeById(){
       findByIdApi(this.defaultDictId).then(resp => {

         //响应式的对象赋值
         this.$set( this.queryParam, 'dictType', resp.data.dictType )

         //普能式的为对象添加属性， 迟早会有问题
        // this.queryParam.dictType = resp.data.dictType

         //重新查询数据
         this.fetchData()
       })
     },
     // 回到字典界面
     handleClose(){
       // https://router.vuejs.org/zh/guide/essentials/navigation.html#%E5%AF%BC%E8%88%AA%E5%88%B0%E4%B8%8D%E5%90%8C%E7%9A%84%E4%BD%8D%E7%BD%AE
       this.$router.replace({ path: '/system/dict' })
     },
     // 新增对话框之前，赋值
     preAddHandler(){
       // 保存到数据库字典类型
       this.form.dictType = this.queryParam.dictType
       //保存当前的字典名称
       this.currentDictName= ''
       // 从字典列中查询（减少对数据库查询）
       if(this.dictTypeOptions && this.dictTypeOptions.length>0 ){
           const arr=  this.dictTypeOptions.filter(item => { return item.dictType ===this.queryParam.dictType })
           if(arr && arr.length>0){
             this.currentDictName = arr[0].dictName  + '-'  +  this.queryParam.dictType
           }
        }
     }

    },
    created(){
      this.loadDictType()
      this.initFormValue()

      // 初始化取得参数
      this.defaultDictId = this.$route.params && this.$route.params.dictId
      // 根据dictId 查询值
      this.loadDictTypeById()

    }

  }
</script>

<style>
</style>
